package cn.tiancaifan.micro.service;


import cn.tiancaifan.micro.entity.*;
import cn.tiancaifan.micro.entity.statistic.Static;
import cn.tiancaifan.micro.mapper.StudentMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    public SystemResults statisticByGender(){
        List<Static> aStatics = studentMapper.statisticByGender();
        return SystemResults.ok("男女统计", aStatics);
    }

    public SystemResults statisticByDepartment(){
        List<Static> aStatics = studentMapper.statisticByDepartment();
        return SystemResults.ok("部门统计", aStatics);
    }

    public RespPageBean getStudentByPage(Student student, Integer page, Integer size) {
        if (page != null && size != null){
            page = (page-1)*size;
        }
        List<Student> data = studentMapper.getStudentByPage(student,page,size);
        Long total = studentMapper.getTotal(student);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(data);
        respPageBean.setTotal(total);
        return respPageBean;
    }

    public SystemResults getNations() {
        List<Nation> data =  studentMapper.getNations();
        return SystemResults.ok(null,data);
    }

    public SystemResults getJobs() {
        List<Job> data =  studentMapper.getJobs();
        return SystemResults.ok(null,data);
    }

    public SystemResults getPoliticsstatus() {
        List<Politicsstatus> data =  studentMapper.getPoliticsstatus();
        return SystemResults.ok(null,data);
    }

    public SystemResults getDeps() {
        List<Department> data =  studentMapper.getDeps();
        return SystemResults.ok(null,data);
    }

    public List<Position> getPositions() {
        return studentMapper.getPositions();
    }

    public int deleteStudentById(Integer id) {
        return studentMapper.deleteStudentById(id);
    }

    public SystemResults addStu(Student student) {
        int i = studentMapper.addStu(student);
        if (i!=0){
            rabbitTemplate.convertAndSend("mail.tiancaifan.welcome",student);
            return SystemResults.ok("添加成功！");
        }else {
            return SystemResults.error("添加失败，请联系管理员！");
        }
    }

    public SystemResults maxWorkID() {
        Integer integer = studentMapper.maxWorkID();
        if (integer!=0){
            return SystemResults.ok(null,integer);
        }else {
            return SystemResults.error("学号生成失败！");
        }
    }

    public SystemResults updateStudentById(Student student) {
        Integer integer = studentMapper.updateStudentById(student);
        if (integer!=0){
            return SystemResults.ok("修改成功");
        }else {
            return SystemResults.error("修改失败！");
        }
    }

    public SystemResults addStus(List<Student> students){
        for (Student student : students) {
            rabbitTemplate.convertAndSend("mail.tiancaifan.welcome",student);
        }
        int i = studentMapper.addStus(students);
        if (i == students.size()){
            return SystemResults.ok("添加成功！");
        }else {
            return SystemResults.error("添加失败，请联系管理员！");
        }
    }
}
